package com.winway.sixbook.ui;

import com.winway.sixbook.R;
import com.winway.sixbook.custom.CloseActivityClass;
import com.winway.sixbook.service.CounterDataManager;

import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;


public class SetCounterListActivity extends Activity {
	
	private static final String TAG = "SetCounterListActivity";
	ListView listView = null;
	private CounterDataManager counterDataManager;
	
	private ImageButton top_tbn_fanhui;
	private Button top_tbn_xinzeng;
	private TextView title;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setcounteractivity);
		CloseActivityClass.activityList.add(this);
		
		if(counterDataManager==null){
			counterDataManager= new CounterDataManager(this);
			counterDataManager.openDataBase();
		}
		listView = (ListView)findViewById(R.id.setcounterlist);
		top_tbn_fanhui = (ImageButton)findViewById(R.id.nagivate_btn_left);
        top_tbn_xinzeng = (Button)findViewById(R.id.navigate_btn_right);
        title = (TextView)findViewById(R.id.title);
        
        top_tbn_fanhui.setOnClickListener(mListener);
        title.setText("计数器类型设置");
        top_tbn_xinzeng.setOnClickListener(mListener); 
		
		Cursor cursor = counterDataManager.fetchAllCounterDatas();
		startManagingCursor(cursor);
		
		ListAdapter listAdapter = new SimpleCursorAdapter(this, R.layout.setcounterlist_item, 
                cursor,
                new String[]{"countname","countnum","_id","targetnum","begindate","enddate"}, 
				new int[]{R.id.setcounterlist_item_digest,R.id.setcounterlist_item_typename,R.id.sethidenid,R.id.tv_targetnum,R.id.tv_begintime,R.id.tv_endtime});
                  
		listView.setAdapter(listAdapter);
		listView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
					TextView t=(TextView)arg1.findViewById(R.id.sethidenid);
					if(t!=null&&t.getText()!=null){
						Intent intent = new Intent(SetCounterListActivity.this, EditCounterActivity.class);
						intent.putExtra("id", t.getText());
						startActivity(intent);
				}
				
				Log.v(TAG, "click:::"+t.getText());				
			}
		});
		
		stopManagingCursor(cursor);
	}
	
	OnClickListener mListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.navigate_btn_right:
					Intent intent = new Intent(SetCounterListActivity.this, EditCounterActivity.class);
					startActivity(intent);
					finish();
				break; 
				case R.id.nagivate_btn_left:
					Intent intent1 = new Intent(SetCounterListActivity.this, SetActivity.class);
					startActivity(intent1);
					finish();
				break;  
			}
		}
	};

	@Override
	public void startManagingCursor(Cursor c) {
		// TODO Auto-generated method stub
		if (Build.VERSION.SDK_INT < 11) { 
			super.startManagingCursor(c);
		}
	}
	
	@Override
	protected void onResume() {
		if (counterDataManager == null) {
			counterDataManager = new CounterDataManager(this);
			counterDataManager.openDataBase();
        }
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (counterDataManager != null) {
			counterDataManager.closeDataBase();
			counterDataManager = null;
        }
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (counterDataManager != null) {
			counterDataManager.closeDataBase();
			counterDataManager = null;
        }
		super.onPause();
	}
	
	/**
	 * 捕捉back
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent counter1 = new Intent(SetCounterListActivity.this, SetActivity.class);
			startActivity(counter1);
			finish();
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
}
